// pages/game/game.js
var roomname,renshu,openid,roomid,acidacid,loadtimes=0,ismaster=0,Dynamic=2,sitesite
var locked=0,fenzu
Page({

  data: {
    statusBarHeight:0,openid:'',btntxt:'准备',btncolor:'#F2B110',loadtimer:'',
    renshu:'',roomname:'',Dynamic:0,select1:true,role2:[],role1:[],
    message1:[
      {  id:0,  img:'../image/dt/dianzan.png',  name:'yy',  biaozhi:'组员',  show:true },
      {  id:1,  img:'../image/dt/dianzan.png',  name:'hh',  biaozhi:'组员',  show:false},
      {  id:2,  img:'../image/dt/dianzan.png',  name:'ss',  biaozhi:'组员',  show:false},
      {  id:3,  img:'../image/dt/dianzan.png',  name:'hh',  biaozhi:'组员',  show:false},
      {  id:4,  img:'../image/dt/dianzan.png',  name:'ss',  biaozhi:'组员',  show:false}
    ],
  },
  yq:function(){
    wx.showToast({
      title: '邀请已发送',
    })
  },
  onLoad: function (options) {
    var that = this;
    openid = wx.getStorageSync('openid')
    this.setData({openid:openid})
    // 可以从创建房间过来，也可以从房间列表过来，都有参数,用于页面显示
    roomid = options.id
    roomname = options.name
    renshu = options.renshu
    acidacid = options.acid
    sitesite = options.site
    this.setData({renshu:renshu,roomid:roomid})
    
    this.loaduser()
    that.loadtimer = setInterval(function(){
      that.loaduser()
      wx.request({
        url: 'https://www.juquduo.com/juquduo/gaming/constructed/message',
        data:{
          activityId:acidacid,
          roomId:roomid
        },
        success(res){
          console.log('检测房间状态',res)
          if(res.data.Code==200){
            wx.redirectTo({
              url: '../dgame1/dgame1?id='+acidacid+'&site='+sitesite+'&roomid='+roomid+'&fenzu='+fenzu,
            })
          }
        }
      })
    },3000)
    // 顶部手机状态栏的宽度
    wx.getSystemInfo({  
      success: e => {  
        // console.log(e)
        that.setData({
          statusBarHeight:e.statusBarHeight
        })
      }  
    })
  },
  onHide(){
    clearInterval(this.loadtimer)
  },
  onUnload(){
    clearInterval(this.loadtimer)
  },
  loaduser(){
    var that = this
    wx.request({
      url: 'https://www.juquduo.com/juquduo/gaming/constructed/players',
      data:{
        activityId:acidacid,
        roomId:roomid
      },
      success(res){
        var userlist = res.data
        console.log(userlist)
        var role1=[],role2=[]
        // 获取玩家分组情况，分2个数组显示。获取自己当前组数，修改顶部样式，
        for(var i = 0;i<userlist.length;i++){
          // 是房主，按钮要显示 开始游戏 (判断：房主的openid等于自己的openid)
          if(userlist[i].Master=='1'&&userlist[i].Openid==openid){
            that.setData({btntxt:'开始游戏'})
            console.log('是房主')
            ismaster=1
          // }else{
          //   that.setData({btntxt:'准备'})
          //   console.log('不是房主')
          //   ismaster=0
          }
          //判断自己已点确定
          if(userlist[i].Readiness=='1'&&userlist[i].Openid==openid){
            locked = 1
          }
            // 判断自己的openid在1组
          if(userlist[i].Role=='1'){
            if(userlist[i].Openid==openid){
              that.setData({select1:true})
              fenzu = 1
            }
            role1.push(userlist[i])
          }
            // 判断自己的openid在2组
          if(userlist[i].Role=='2'){
            if(userlist[i].Openid==openid){
              that.setData({select1:false})
              fenzu = 2
            }
            role2.push(userlist[i])
          }
        }
        that.setData({role1:role1,role2:role2,Dynamic:userlist.length})
        Dynamic=userlist.length
      }
    })
  },
  turnto2(){
    var that = this
    if(locked==0){
      this.setData({select1:false})
      fenzu = 2
      wx.request({
        url: 'https://www.juquduo.com/juquduo/gaming/constructed/adjust',
        data:{
          activityId:acidacid,
          roomId:roomid,
          openid:openid,
          role:2
        },
        success(res){
          console.log(res)
          that.loaduser()
        }
      })
    }else{
      wx.showToast({
        title: '已锁定，无法换组',
      })
    }
  },
  turnto1(){
    var that = this
    if(locked==0){

      this.setData({select1:true})
      fenzu = 1
      wx.request({
        url: 'https://www.juquduo.com/juquduo/gaming/constructed/adjust',
        data:{
          activityId:acidacid,
          roomId:roomid,
          openid:openid,
          role:1
        },
        success(res){
          console.log(res)
          that.loaduser()
        }
      })
    }else{
      wx.showToast({
        title: '已锁定，无法换组',
      })
    }
  },
  copynum(){
    // 复制功能
    wx.setClipboardData({
      data: roomid,
      success: function (res) {
        wx.getClipboardData({
          success: function (res) {
            wx.showToast({
              title: '复制成功'
            })}})}
    })
  },
  tuichu(){
    wx.showModal({
      title:'确定要退出房间吗？',
      success(res){
        if (res.confirm) {
          wx.request({
            url: 'https://www.juquduo.com/juquduo/gaming/constructed/exit',
            data:{
              roomId:roomid,
              openid:openid,
              activityId:acidacid
            },
            success(res){
              console.log(res)
              wx.redirectTo({
                url: './dacmode',
              })
            }
          })
        }
      }
    })
    
  },
  startbtn(){
    var that = this
    var ispingheng = 0
    var isallin = 0
    if(ismaster=='1'){
      if(that.data.role1.length!=that.data.role2.length){
        wx.showToast({
          icon:'none',
          title: '分组人数不平衡',
        })
        ispingheng=0
      }else{ispingheng=1}
      
      if(Dynamic!=renshu){
        wx.showToast({
          icon:'none',
          title: '请等待所有玩家进入房间',
        })
        isallin=0
      }else{isallin=1}

      if(ispingheng==1&&isallin==1){
        wx.request({
          url: 'https://www.juquduo.com/juquduo/gaming/constructed/start',
          data:{
            activityId:acidacid,
            roomId:roomid
          },
          success(res){
            console.log('点击开始游戏：',res)
            if(res.data.Code==300){
              console.log('start返回值',res.data)
              wx.showToast({
                icon:'none',
                title: '请等待所有玩家准备',
              })
            }
            if(res.data.Code==200){
              console.log('start返回值',res.data)
              clearInterval(that.loadtimer)
              wx.redirectTo({
                url: '../dgame1/dgame1?id='+acidacid+'&site='+sitesite+'&roomid='+roomid+'&fenzu='+fenzu,
              })
            }
          }
        })
      }
    }else{
      that.setData({btntxt:'请等待游戏开始',btncolor:'#F8D780'})
      wx.request({
        url: 'https://www.juquduo.com/juquduo/gaming/constructed/ready?',
        data:{
          activityId:acidacid,
          roomId:roomid,
          openid:openid
        },
        success(res){
          console.log('玩家已点锁定',res)
          locked = 1
          that.loaduser()
        }
      })
    }
  }
})